package com.website.system.service.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.website.system.dao.ComputerDetailsMapper;
import com.website.system.model.ComputerDetails;
import com.website.system.service.ComputerDetailsService;
import com.website.system.utils.MyException;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

@Service
public class ComputerDetailsServiceImpl extends ServiceImpl<ComputerDetailsMapper, ComputerDetails> implements ComputerDetailsService{
@Resource
private ComputerDetailsMapper computerDetailsMapper;
    @Override
    public List<ComputerDetails> findByCon() {
        return computerDetailsMapper.findByCon();
    }

    @Transactional(readOnly = false,rollbackFor = Exception.class)
    @Override
    public boolean batchImport(String fileName, MultipartFile file) throws Exception {
        boolean notNull = false;
        List<ComputerDetails> userList = new ArrayList<>();
        if (!fileName.matches("^.+\\.(?i)(xls)$") && !fileName.matches("^.+\\.(?i)(xlsx)$")) {
            throw new MyException("上传文件格式不正确");
        }
        boolean isExcel2003 = true;
        if (fileName.matches("^.+\\.(?i)(xlsx)$")) {
            isExcel2003 = false;
        }
        InputStream is = file.getInputStream();
        Workbook wb = null;
        if (isExcel2003) {
            wb = new HSSFWorkbook(is);
        } else {
            wb = new XSSFWorkbook(is);
        }
        Sheet sheet = wb.getSheetAt(0);
        if(sheet!=null){
            notNull = true;
        }
        ComputerDetails user;
        for (int r = 2; r <= sheet.getLastRowNum(); r++) {//r = 2 表示从第三行开始循环 如果你的第三行开始是数据
            Row row = sheet.getRow(r);//通过sheet表单对象得到 行对象
            if (row == null){
                continue;
            }

            //sheet.getLastRowNum() 的值是 10，所以Excel表中的数据至少是10条；不然报错 NullPointerException
            user = new ComputerDetails();
            if( row.getCell(0).getCellType() !=1){//循环时，得到每一行的单元格进行判断
                throw new MyException("导入失败(第"+(r+1)+"行,用户名请设为文本格式)");
            }
            String pid = row.getCell(0).getStringCellValue();//得到每一行第一个单元格的值
            if(pid == null || pid.isEmpty()){//判断是否为空
                throw new MyException("导入失败(第"+(r+1)+"行,类别名称未填写)");
            }
            row.getCell(1).setCellType(Cell.CELL_TYPE_STRING);//得到每一行的 第二个单元格的值
            String dname = row.getCell(1).getStringCellValue();
            if(dname==null || dname.isEmpty()){
                throw new MyException("导入失败(第"+(r+1)+"行,名称未填写)");
            }
            String color = row.getCell(2).getStringCellValue();
            if(color==null || color.isEmpty()){
                throw new MyException("导入失败(第"+(r+1)+"行,颜色未填写)");
            }

            String createTime =  row.getCell(3).getStringCellValue();
            if(createTime==null){
                throw new MyException("导入失败(第"+(r+1)+"行,生产日期未填写)");
            }
            String picture = row.getCell(4).getStringCellValue();
            if(picture==null || picture.isEmpty()){
                throw new MyException("导入失败(第"+(r+1)+"行,图片未填写)");
            }
            String shellTime = row.getCell(5).getStringCellValue();
            if(shellTime==null || picture.isEmpty()){
                throw new MyException("导入失败(第"+(r+1)+"行,出售时间未填写)");
            }
            String dtype = row.getCell(5).getStringCellValue();
            if(dtype==null || picture.isEmpty()){
                throw new MyException("导入失败(第"+(r+1)+"行,虚拟列未填写)");
            }

            //完整的循环一次 就组成了一个对象
            user.setPid(pid);
            user.setDname(dname);
            user.setColor(color);
            user.setCreateTime(createTime);
            user.setPicture(picture);
            user.setShellTime(shellTime);
            userList.add(user);
        }
        for (ComputerDetails userResord : userList) {
            String name = userResord.getDname();
            int cnt = computerDetailsMapper.selectByComputerDetailsName(name);
            if (cnt == 0) {
                computerDetailsMapper.addComputerDetails(userResord);
                System.out.println(" 插入 "+userResord);
            } else {
                computerDetailsMapper.updateComputerDetailsByName(userResord);
                System.out.println(" 更新 "+userResord);
            }
        }
        return notNull;
    }
}
